package top.wshape1.shiziapi.message.controller;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import top.wshape1.shiziapi.common.constant.AuthConstant;
import top.wshape1.shiziapi.common.domain.CommonUser;
import top.wshape1.shiziapi.common.domain.to.ProjectUserIdTo;
import top.wshape1.shiziapi.common.utils.Base64Util;
import top.wshape1.shiziapi.common.utils.R;
import top.wshape1.shiziapi.common.domain.vo.MessageVo;
import top.wshape1.shiziapi.message.domain.MessageUser;
import top.wshape1.shiziapi.message.service.MessageBroadcastService;
import top.wshape1.shiziapi.message.service.MessageUserService;

import java.util.List;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-10-02
 */

@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private MessageUserService messageUserService;

    @Resource
    private MessageBroadcastService messageBroadcastService;

    /**
     * 用户已读某条信息 广播或私信消息
     *
     * @param msgType
     * @param msgId
     * @param userBase64
     * @return
     */
    @GetMapping("/read/{type}")
    public R<Void> readMessage(@PathVariable("type") String msgType,
                               @RequestParam("msgId") Long msgId,
                               @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);
        Long userId = commonUser.getId();

        if ("broadcast".equals(msgType)) {
            messageBroadcastService.read(msgId, userId);
        } else {
            messageUserService.read(msgId, userId);
        }

        return R.ok();
    }

    /**
     * 获取用户未读消息的数量
     *
     * @param userBase64
     * @return
     */
    @GetMapping("/count/unread")
    public R<Long> countMessage(@RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        long cnt1 = messageBroadcastService.countUnread(commonUser.getId());
        long cnt2 = messageUserService.countUnread(commonUser.getId());

        return R.ok(cnt1 + cnt2);
    }

    /**
     * 获取用户不同类型的历史消息
     *
     * @param type
     * @param userBase64
     * @return
     */
    @GetMapping("/get/{type}")
    public R<List<MessageVo>> getMsg(@PathVariable("type") String type,
                                     @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);
        Long userId = commonUser.getId();
        List<MessageVo> list = null;

        if ("private".equals(type)) {
            list = messageUserService.getPrivateMsg(userId);
        } else if ("invite".equals(type)) {
            list = messageUserService.getInviteMsg(userId);
        } else if ("notify".equals(type)) {
            list = messageBroadcastService.getNotifyMsg(userId);
        }

        return R.ok(list);

    }

    /**
     * 发送项目邀请消息
     *
     * @param projectId
     * @param projectName
     * @param userId
     * @param userName
     * @param memberId
     * @return
     */
    @GetMapping("/send/invite")
    public R<Void> sendInviteMsg(@RequestParam("projectId") Long projectId,
                                 @RequestParam("projectName") String projectName,
                                 @RequestParam("userId") Long userId,
                                 @RequestParam("userName") String userName,
                                 @RequestParam("memberId") Long memberId
    ) {

        messageUserService.sendInviteMsg(projectId, projectName, userId, userName, memberId);

        return R.ok();
    }

    @GetMapping("/send/reject")
    public R<Void> sendRejectMsg(@RequestParam("projectId") Long projectId,
                                 @RequestParam("projectName") String projectName,
                                 @RequestParam("memberId") Long memberId,
                                 @RequestParam("memberName") String memberName,
                                 @RequestParam("userId") Long userId
                                 ) {

        messageUserService.sendRejectMsg(projectId, projectName, memberId, memberName, userId);

        return R.ok();
    }

    /**
     * 获取邀请消息对应的项目ID和邀请人消息
     * @param msgId
     * @return
     */
    @GetMapping("/getProjectUserId")
    public R<ProjectUserIdTo> getProjectId(@RequestParam("msgId") Long msgId) {
        ProjectUserIdTo to = messageUserService.getAndParseId(msgId);
        return R.ok(to);
    }

}
